package com.f5.management.service.impl;

import com.f5.management.dao.AdminUserDAO;
import com.f5.management.dao.PermissionDAO;
import com.f5.management.dao.RoleDAO;
import com.f5.management.entity.AdminUser;
import com.f5.management.entity.Permission;
import com.f5.management.entity.dto.AdminUserAndRoleAndPermissionDto;
import com.f5.management.entity.dto.AdminUserAndRoleDto;
import com.f5.management.service.AdminUserService;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminUserServiceImpl
 * @Description AdminUserService接口实现类
 * @Author 吴
 * @Date 2021/4/2 15:59
 * @Version 1.0
 **/

@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    AdminUserDAO adminUserDAO;
    @Autowired
    PermissionDAO permissionDAO;
    @Autowired
    RoleDAO roleDAO;


    //查询得到一个管理员用户
    @Override
    public AdminUser getAdminUser(String account) {
        AdminUser adminUser = adminUserDAO.getAdminUser(account);
        return adminUser;
    }

    //根据时间查询用户角色和权限 (模糊查询)
    @Override
    public List<AdminUser> getAdminUserLists(AdminUserAndRoleDto adminDto) {
        return adminUserDAO.getAdminUserLists(adminDto);
    }

    //根据传入的信息修改用户状态
    @Override
    public void updateAdminUserState(AdminUser adminUser) {

        adminUserDAO.updateAdminUserState(adminUser);
    }


    //向数据库增加一个user用户，并插入角色信息，添加了事务管理
    @Override
    @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED ,rollbackFor = Exception.class)
    public String insertUser(AdminUser adminUser,long id,String truthPasswrod){

        if(adminUser.getPassword().equals(null) ||
           adminUser.getAccount().equals(null)  ||
           adminUser.getEmail().equals(null)    ||
           adminUser.getNumber().equals(null)   ||
           adminUser.getName().equals(null)) {
            return "输入的信息不完全";
        }else {




            if (!adminUser.getPassword().equals(truthPasswrod) ){
                return "两次输入的密码不正确";
            }else if (adminUserDAO.selectAccount(adminUser.getAccount())!=null){

                return "账号已经存在";

            }else{


                adminUser.setJoinTime(new Date());
                adminUser.setRid(id);
                adminUser.setUserState(1);
                adminUser.setDeleteAccount("正常使用");
            }







                adminUserDAO.insertAdminUser(adminUser);
                adminUserDAO.insertUserRole(adminUserDAO.selectId(adminUser.getAccount()),id);


                return "添加成功";


        }
    }



    //假删除功能
    @Override
    public String deleteAdminUser(String account) {

        adminUserDAO.deleteAccount(account);

        return "删除成功";
    }
}
